package com.leyou.item.controller;

import com.leyou.item.dto.BrandDTO;
import com.leyou.item.dto.PageDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 根据品牌相关的分类id集合新增
     *
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> insertBrandByIds(BrandDTO brandDTO) {
        brandService.insertBrandByIds(brandDTO);

        //使用建造者模式创建返回的ResponseEntity对象
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据传入的DTO对象修改品牌
     *
     * @param brandDTO 品牌对象，里面封装了分类id集合
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO) {
        brandService.updateBrand(brandDTO);
        //使用建造者模式创建返回的ResponseEntity对象
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据品牌id集合查询品牌集合
     *
     * @param ids 品牌id集合
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> findByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(brandService.findByIds(ids));
    }


    /**
     * 根据分类id查询品牌集合
     *
     * @return
     */
    @GetMapping("of/category")
    public ResponseEntity<List<BrandDTO>> findById(@RequestParam("id") Long cid) {
        return ResponseEntity.ok(brandService.findById(cid));
    }

    /**
     * 品牌分页
     * @param key 搜索条件
     * @param page 当前页
     * @param rows 页面大小
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> findPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", required = false) Long page,
            @RequestParam(value = "rows", required = false) Long rows) {
        return ResponseEntity.ok(brandService.findPage(key, page, rows));
    }

    /**
     * 根据品牌id查询品牌信息
     * @param id 品牌id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> findByBrandId(@PathVariable("id")Long id)
    {
        return ResponseEntity.ok(brandService.findByBrandId(id));
    }


    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteByBrandId(@PathVariable("id")Long id)
    {
        brandService.deleteByBrandId(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
